<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>收款收据信息录入</span>
      <el-button type="default" @click="goBack">返回</el-button>
    </div>

    <el-form
      :model="dynamicValidateForm"
      :rules="rules"
      ref="dynamicValidateForm"
      label-position="left"
      label-width="100px"
      class="demo-dynamic"
    >
      <el-form-item label="交款方" prop="people">
        <el-input v-model="dynamicValidateForm.people"></el-input>
      </el-form-item>
      <el-form-item label="开票日期" prop="day">
        <el-date-picker type="date" v-model="dynamicValidateForm.day" style="width: 100%;"></el-date-picker>
      </el-form-item>
      <el-form-item label="收款方" prop="dep">
        <el-input v-model="dynamicValidateForm.dep"></el-input>
      </el-form-item>
      <el-form-item label="开票人" prop="user">
        <el-input v-model="dynamicValidateForm.user"></el-input>
      </el-form-item>
      <el-form-item label="收款人" prop="name">
        <el-input v-model="dynamicValidateForm.name"></el-input>
      </el-form-item>

      <el-row v-for="(domain, index) in dynamicValidateForm.domains" :key="domain.key">
        <el-divider></el-divider>
        <div class="clearfix">
          <span>收款明细 {{index+1}}</span>
          <el-button type="danger" @click.prevent="removeDomain(domain)" plain>删除</el-button>
        </div>
        <el-divider></el-divider>
        <el-form-item
          label="收款金额"
          :rules="{ required: true, message: '请输入收款金额', trigger: 'blur' }"
          :prop="'domains.'+ index +'.money'"
        >
          <el-input type="number" v-model="domain.money">
            <template slot="prepend">￥</template>
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item
          label="收款事由"
          :rules="{ required: true, message: '请输入收款事由', trigger: 'blur' }"
          :prop="'domains.'+ index +'.reason'"
        >
          <el-input type="textarea" v-model="domain.reason" :autosize="{ minRows: 4, maxRows: 6}"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            v-model="domain.desc"
            :autosize="{ minRows: 4, maxRows: 6}"
            :prop="'domains.'+ index +'.desc'"
          ></el-input>
        </el-form-item>
      </el-row>
      <el-divider></el-divider>
      <el-row class="sub-btn">
        <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
        <el-button type="success" @click="addDomain">新增收款</el-button>
        <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
      </el-row>
    </el-form>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      ljson: { money: "", reason: "", desc: "" },
      dynamicValidateForm: {
        // 交款方
        people: "",
        // 开票日期
        day: "",
        // 收款方
        dep: "",
        // 开票人
        user: "",
        // 收款人
        name: "",
        // 收款明细
        domains: [
          {
            // 收款金额
            money: "",
            // 事由
            reason: "",
            // 备注
            desc: ""
          }
        ]
      },
      rules: {
        people: [
          { required: true, message: "请输入交款方名称", trigger: "blur" }
        ],
        day: [
          {
            type: "date",
            required: true,
            message: "请选择开票日期",
            trigger: "change"
          }
        ],
        dep: [{ required: true, message: "请输入收款方名称", trigger: "blur" }],
        user: [
          { required: true, message: "请输入开票人名称", trigger: "blur" }
        ],
        name: [{ required: true, message: "请输入收款人名称", trigger: "blur" }]
      }
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let params = new URLSearchParams();
          params.append("people", this.dynamicValidateForm.people);
          params.append("day", this.dynamicValidateForm.day);
          params.append("dep", this.dynamicValidateForm.dep);
          params.append("user", this.dynamicValidateForm.user);
          params.append("name", this.dynamicValidateForm.name);

          let domains = this.dynamicValidateForm.domains;
          let dlist = new Array();
          for (let i = 0; i < domains.length; i++) {
            dlist.push(
              (this.ljson = {
                money: domains[i].money,
                reason: domains[i].reason,
                desc: domains[i].desc
              })
            );
          }
          console.log(dlist);
          dlist = JSON.stringify(dlist);
          params.append("domains", dlist);
          this.$api.post("/invoice", params, r => {
            console.log(r);
          });
          alert("录入成功!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    removeDomain(item) {
      var index = this.dynamicValidateForm.domains.indexOf(item);
      if (index !== -1) {
        this.dynamicValidateForm.domains.splice(index, 1);
      }
    },
    addDomain() {
      this.dynamicValidateForm.domains.push({
        key: Date.now()
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.clearfix {
  span {
    display: block;
    float: left;
    line-height: 40px;
    text-align: left;
  }
  button {
    display: block;
    float: right;
  }
  &:after {
    content: "";
    display: block;
    clear: both;
  }
}
.sub-btn {
  button {
    display: block;
    float: left;
  }
  button:last-child {
    float: right;
  }
  &:after {
    content: "";
    display: block;
    clear: both;
  }
}
</style>
